home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1999 March / EnigmA AMIGA RUN 35 (1999)(G.R. Edizioni)(IT)[!][issue 1999-03].iso / earcd / -archivi / -recent2 / clib37x.lha / CLib37x / source / lib_source / StartUp.c < prev    next >
C/C++ Source or Header  |  1998-03-18  |  10KB  |  307 lines

  1. /*
  2. **      $VER: StartUp.c 37.31 (18.3.98)
  3. **
  4. **      Library startup-code and function table definition
  5. **
  6. **      (C) Copyright 1996-98 Andreas R. Kleinert
  7. **      All Rights Reserved.
  8. */
  9.  
  10. #define __USE_SYSBASE        // perhaps only recognized by SAS/C
  11.  
  12. #include <exec/types.h>
  13. #include <exec/memory.h>
  14. #include <exec/libraries.h>
  15. #include <exec/execbase.h>
  16. #include <exec/resident.h>
  17. #include <exec/initializers.h>
  18.  
  19.  
  20. #ifdef __MAXON__
  21. #include <pragma/exec_lib.h>
  22. #include <linkerfunc.h>
  23. #else
  24. #include <proto/exec.h>    // all other compilers
  25. #endif
  26. #include "compiler.h"
  27.  
  28.  
  29. #ifdef __GNUC__
  30. #include "../include/example/examplebase.h"
  31. #elif VBCC
  32. #include "include/example/examplebase.h"
  33. #else
  34. #include "/include/example/examplebase.h"
  35. #endif
  36. #include "SampleFuncs.h"
  37.  
  38.  
  39. extern ULONG __saveds __stdargs L_OpenLibs(struct ExampleBase *ExampleBase);
  40. extern void  __saveds __stdargs L_CloseLibs(void);
  41.  
  42. struct ExampleBase * __saveds ASM InitLib( register __a6 struct ExecBase    *sysbase GNUCREG(a6),
  43.                                            register __a0 SEGLISTPTR          seglist GNUCREG(a0),
  44.                                            register __d0 struct ExampleBase *exb     GNUCREG(d0));
  45. struct ExampleBase * __saveds ASM OpenLib( register __a6 struct ExampleBase *ExampleBase GNUCREG(a6));
  46. SEGLISTPTR __saveds ASM CloseLib( register __a6 struct ExampleBase *ExampleBase GNUCREG(a6));
  47. SEGLISTPTR __saveds ASM ExpungeLib( register __a6 struct ExampleBase *exb GNUCREG(a6));
  48. ULONG ASM ExtFuncLib(void);
  49.  
  50.  
  51. /* ----------------------------------------------------------------------------------------
  52.    ! LibStart:
  53.    !
  54.    ! If someone tries to start a library as an executable, it must return (LONG) -1
  55.    ! as result. That's what we are doing here.
  56.    ---------------------------------------------------------------------------------------- */
  57.  
  58. LONG ASM LibStart(void)
  59. {
  60.  return(-1);
  61. }
  62.  
  63.  
  64. /* ----------------------------------------------------------------------------------------
  65.    ! Function and Data Tables:
  66.    !
  67.    ! The function and data tables have been placed here for traditional reasons.
  68.    ! Placing the RomTag structure before (-> LibInit.c) would also be a good idea,
  69.    ! but it depends on whether you would like to keep the "version" stuff separately.
  70.    ---------------------------------------------------------------------------------------- */
  71.  
  72. extern APTR FuncTab [];
  73. /*  extern struct MyDataInit DataTab;  */
  74. extern DataTab; /* DICE fix */
  75.                                   /* Instead you may place ROMTag + Datatab directly, here */
  76.                                   /* (see LibInit.c). This may fix "Installer" version     */
  77.                                   /* checking problems, too - try it.                      */
  78.  
  79. struct InitTable                       /* do not change */
  80. {
  81.  ULONG              LibBaseSize;
  82.  APTR              *FunctionTable;
  83.  struct MyDataInit *DataTable;
  84.  APTR               InitLibTable;
  85. } InitTab =
  86. {
  87.  (ULONG)               sizeof(struct ExampleBase),
  88.  (APTR              *) &FuncTab[0],
  89.  (struct MyDataInit *) &DataTab,
  90.  (APTR)                InitLib
  91. };
  92.  
  93. APTR FuncTab [] =
  94. {
  95.  OpenLib,
  96.  CloseLib,
  97.  ExpungeLib,
  98.  ExtFuncLib,
  99.  
  100.  EXF_TestRequest,  /* add your own functions here */
  101.  
  102.  (APTR) ((LONG)-1)
  103. };
  104.  
  105.  
  106. extern struct ExampleBase *ExampleBase;
  107.  
  108. /* ----------------------------------------------------------------------------------------
  109.    ! InitLib:
  110.    !
  111.    ! This one is single-threaded by the Ramlib process. Theoretically you can do, what
  112.    ! you like here, since you have full exclusive control over all the library code and data.
  113.    ! But due to some bugs in Ramlib V37-40, you can easily cause a deadlock when opening
  114.    ! certain libraries here (which open other libraries, that open other libraries, that...)
  115.    !
  116.    ---------------------------------------------------------------------------------------- */
  117.  
  118. struct ExampleBase * __saveds ASM InitLib( register __a6 struct ExecBase      *sysbase GNUCREG(a6),
  119.                                            register __a0 SEGLISTPTR            seglist GNUCREG(a0),
  120.                                            register __d0 struct ExampleBase   *exb     GNUCREG(d0))
  121. {
  122.  ExampleBase = exb;
  123.  
  124.  ExampleBase->exb_SysBase = sysbase;
  125.  ExampleBase->exb_SegList = seglist;
  126.  
  127.  if(L_OpenLibs(ExampleBase)) return(ExampleBase);
  128.  
  129.  L_CloseLibs();
  130.  
  131.   {
  132.    ULONG negsize, possize, fullsize;
  133.    UBYTE *negptr = (UBYTE *) ExampleBase;
  134.  
  135.    negsize  = ExampleBase->exb_LibNode.lib_NegSize;
  136.    possize  = ExampleBase->exb_LibNode.lib_PosSize;
  137.    fullsize = negsize + possize;
  138.    negptr  -= negsize;
  139.  
  140.    FreeMem(negptr, fullsize);
  141.  
  142.    #ifdef __MAXON__
  143.    CleanupModules();
  144.    #endif
  145.   }
  146.  
  147.  return(NULL);
  148. }
  149.  
  150. /* ----------------------------------------------------------------------------------------
  151.    ! OpenLib:
  152.    !
  153.    ! This one is enclosed within a Forbid/Permit pair by Exec V37-40. Since a Wait() call
  154.    ! would break this Forbid/Permit(), you are not allowed to start any operations that
  155.    ! may cause a Wait() during their processing. It's possible, that future OS versions
  156.    ! won't turn the multi-tasking off, but instead use semaphore protection for this
  157.    ! function.
  158.    !
  159.    ! Currently you only can bypass this restriction by supplying your own semaphore
  160.    ! mechanism.
  161.    ---------------------------------------------------------------------------------------- */
  162.  
  163. struct ExampleBase * __saveds ASM OpenLib( register __a6 struct ExampleBase *ExampleBase GNUCREG(a6))
  164. {
  165.  #ifdef __MAXON__
  166.  GetBaseReg();
  167.  InitModules();
  168.  #endif
  169.  
  170.  ExampleBase->exb_LibNode.lib_OpenCnt++;
  171.  
  172.  ExampleBase->exb_LibNode.lib_Flags &= ~LIBF_DELEXP;
  173.  
  174.  return(ExampleBase);
  175. }
  176.  
  177. /* ----------------------------------------------------------------------------------------
  178.    ! CloseLib:
  179.    !
  180.    ! This one is enclosed within a Forbid/Permit pair by Exec V37-40. Since a Wait() call
  181.    ! would break this Forbid/Permit(), you are not allowed to start any operations that
  182.    ! may cause a Wait() during their processing. It's possible, that future OS versions
  183.    ! won't turn the multi-tasking off, but instead use semaphore protection for this
  184.    ! function.
  185.    !
  186.    ! Currently you only can bypass this restriction by supplying your own semaphore
  187.    ! mechanism.
  188.    ---------------------------------------------------------------------------------------- */
  189.  
  190. SEGLISTPTR __saveds ASM CloseLib( register __a6 struct ExampleBase *ExampleBase GNUCREG(a6))
  191. {
  192.  ExampleBase->exb_LibNode.lib_OpenCnt--;
  193.  
  194.  if(!ExampleBase->exb_LibNode.lib_OpenCnt)
  195.   {
  196.    if(ExampleBase->exb_LibNode.lib_Flags & LIBF_DELEXP)
  197.     {
  198.      return( ExpungeLib(ExampleBase) );
  199.     }
  200.   }
  201.  
  202.  return(NULL);
  203. }
  204.  
  205. /* ----------------------------------------------------------------------------------------
  206.    ! ExpungeLib:
  207.    !
  208.    ! This one is enclosed within a Forbid/Permit pair by Exec V37-40. Since a Wait() call
  209.    ! would break this Forbid/Permit(), you are not allowed to start any operations that
  210.    ! may cause a Wait() during their processing. It's possible, that future OS versions
  211.    ! won't turn the multi-tasking off, but instead use semaphore protection for this
  212.    ! function.
  213.    !
  214.    ! Currently you only could bypass this restriction by supplying your own semaphore
  215.    ! mechanism - but since expunging can't be done twice, one should avoid it here.
  216.    ---------------------------------------------------------------------------------------- */
  217.  
  218. SEGLISTPTR __saveds ASM ExpungeLib( register __a6 struct ExampleBase *exb GNUCREG(a6))
  219. {
  220.  struct ExampleBase *ExampleBase = exb;
  221.  SEGLISTPTR seglist;
  222.  
  223.  if(!ExampleBase->exb_LibNode.lib_OpenCnt)
  224.   {
  225.    ULONG negsize, possize, fullsize;
  226.    UBYTE *negptr = (UBYTE *) ExampleBase;
  227.  
  228.    seglist = ExampleBase->exb_SegList;
  229.  
  230.    Remove((struct Node *)ExampleBase);
  231.  
  232.    L_CloseLibs();
  233.  
  234.    negsize  = ExampleBase->exb_LibNode.lib_NegSize;
  235.    possize  = ExampleBase->exb_LibNode.lib_PosSize;
  236.    fullsize = negsize + possize;
  237.    negptr  -= negsize;
  238.  
  239.    FreeMem(negptr, fullsize);
  240.  
  241.    #ifdef __MAXON__
  242.    CleanupModules();
  243.    #endif
  244.  
  245.    return(seglist);
  246.   }
  247.  
  248.  ExampleBase->exb_LibNode.lib_Flags |= LIBF_DELEXP;
  249.  
  250.  return(NULL);
  251. }
  252.  
  253. /* ----------------------------------------------------------------------------------------
  254.    ! ExtFunct:
  255.    !
  256.    ! This one is enclosed within a Forbid/Permit pair by Exec V37-40. Since a Wait() call
  257.    ! would break this Forbid/Permit(), you are not allowed to start any operations that
  258.    ! may cause a Wait() during their processing. It's possible, that future OS versions
  259.    ! won't turn the multi-tasking off, but instead use semaphore protection for this
  260.    ! function.
  261.    !
  262.    ! Currently you only can bypass this restriction by supplying your own semaphore
  263.    ! mechanism - but since this function currently is unused, you should not touch
  264.    ! it, either.
  265.    ---------------------------------------------------------------------------------------- */
  266.  
  267. ULONG ASM ExtFuncLib(void)
  268. {
  269.  return(NULL);
  270. }
  271.  
  272. struct ExampleBase *ExampleBase = NULL;
  273.  
  274.  
  275. /* ----------------------------------------------------------------------------------------
  276.    ! __SASC stuff:
  277.    !
  278.    ! This is only for SAS/C - its intention is to turn off internal CTRL-C handling
  279.    ! for standard C functions and to avoid calls to exit() et al.
  280.    ---------------------------------------------------------------------------------------- */
  281.  
  282. #ifdef __SASC
  283.  
  284. #ifdef ARK_OLD_STDIO_FIX
  285.  
  286. ULONG XCEXIT       = NULL; /* These symbols may be referenced by    */
  287. ULONG _XCEXIT      = NULL; /* some functions of sc.lib, but should  */
  288. ULONG ONBREAK      = NULL; /* never be used inside a shared library */
  289. ULONG _ONBREAK     = NULL;
  290. ULONG base         = NULL; /* Note, that XCEXIT/ONBREAK actually    */
  291. ULONG _base        = NULL; /* should have been defined as functions */
  292. ULONG ProgramName  = NULL; /* and not as ULONGs...                  */
  293. ULONG _ProgramName = NULL;
  294. ULONG StackPtr     = NULL;
  295. ULONG _StackPtr    = NULL;
  296. ULONG oserr        = NULL;
  297. ULONG _oserr       = NULL;
  298. ULONG OSERR        = NULL;
  299. ULONG _OSERR       = NULL;
  300.  
  301. #endif /* ARK_OLD_STDIO_FIX */
  302.  
  303. void __regargs __chkabort(void) { }  /* a shared library cannot be    */
  304. void __regargs _CXBRK(void)     { }  /* CTRL-C aborted when doing I/O */
  305.  
  306. #endif /* __SASC */
  307.